* Author: Ashley Pandya
* Name: 5_cap_build.do
* Load and merge BEA capital data and employment data to generate "capital deepening" figure A3

* Set Global File Paths
global root = "T:/service_industries/replication_package"
global raw_root = "${root}/datasets/raw"
global intermediate_root = "${root}/datasets/intermediate"
global built_root = "${root}/datasets/built"
global exhibits_root = "${root}/exhibits"

********************************************************************************
*** Part 1: Load in & Merge Quantity Index & Employment Data
********************************************************************************
* Data from the BEA for NAICS 722 (Employment NSA)

************************************
*** 1.1 Bring in quantity index
************************************

* [INSERT RAW BEA FIXED ASSETS DATA HERE] 
import delimited using "${raw_root}/bea_fixed_assets/bea_3_2_esi_qidx", clear varnames(4)

* Clean
drop in 1
forvalues i = 3/35 {
	local year = 1992 + (`i' - 3)
	rename v`i' y`year'
}

keep if line == "95"
drop line v2

* Reshape
gen id = 1
reshape long y, i(id) j(year)
drop id 
rename y qidx

tsset year
tempfile qidx
save `qidx'

************************************
*** 1.2 Merge in Employment Data
************************************

import excel "${raw_root}/bls_emp/food_service_emp_nsa_feb_2026.xlsx", clear firstrow

drop SeriesID

bysort Year: egen Value_yravg = mean(Value)

collapse (mean) Value, by(Year)
rename Value emp
rename Year year

* Merge
merge 1:1 year using `qidx', nogen

********************************************************************************
*** Part 2: Generate key values
* --------  K Index, L Index, K gr. rate, L gr. rate, K/L gr. rate, K/L Index
********************************************************************************

* K Index
egen double qidxbase = max(cond(year==1992, qidx, .))
gen double kidx = 100 * (qidx / qidxbase)
drop qidxbase

* L Index
egen double empbase = max(cond(year==1992, emp, .))
gen double lidx = 100 * (emp / empbase)
drop empbase

* K growth rate
gen double kgrwth = (kidx-L.kidx)/L.kidx

* L growth rate
gen double lgrwth = (lidx-L.lidx)/L.lidx

* K/L growth rate
gen double kl_grwth = kgrwth-lgrwth

* K/L Index
gen double klidx = .
replace klidx = 100 if year == 1992
replace klidx = L.klidx * (1 + kl_grwth) if year > 1992

********************************************************************************
*** Part 3: Save the dataset
********************************************************************************
save "${built_root}/cap_build.dta", replace
